﻿using AutoMapper;
using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProcedureInfoController : ControllerBase
    {
        private readonly IProcedureInfoService svc;
        private readonly IMapper mapper;
        private readonly IBusinessInfoService Bsvc;

        public ProcedureInfoController(IProcedureInfoService svc,IMapper mapper, IBusinessInfoService Bsvc)
        {
            this.svc = svc;
            this.mapper = mapper;
            this.Bsvc = Bsvc;
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("getAll")]
        public Result Get()
        {
            var data = svc.Query(o => o.Status == 1);

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 根据业务获取工序
        /// </summary>
        /// <param name="business"></param>
        /// <returns></returns>
        [HttpGet("getByBusiness")]
        public Result GetByBusiness(int business)
        {
            var data = svc.Query(o=>o.BusinessId == business);
            
            return new Result() { code=1,msg="成功",data = data};
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="procedureName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<ProcedureDto> Get(string procedureName = "",int? businessId = null, int pageSize = 10, int pageIndex = 1)
        {
            string sql = @"select p.*
	                        ,b.BusinessName
	                        from ProcedureInfos p
	                        inner join BusinessInfos b on p.BusinessId = b.Id
	                        where 1=1";

            if (!string.IsNullOrEmpty(procedureName)) sql += " and procedureName like @procedureName";
            if (businessId != null) sql += " and businessId = @businessId";

            string countSql = $"select count(*) from ({sql}) ff";

            int total = svc.ExecuteScalar<int>(countSql, new { procedureName = $"%{procedureName}%", businessId });

            sql += " order by createTime desc";
            sql += " offset @pageSize*(@pageIndex-1) rows fetch next @pageSize rows only";

            var data = svc.Query<ProcedureDto>(sql, new { procedureName = $"%{procedureName}%", businessId,pageSize,pageIndex});

            return new PagedResult<ProcedureDto>() { code = 1, msg = "成功", total = total, data = data };
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(ProcedurePostDto dto)
        {
            var entity = mapper.Map<ProcedureInfo>(dto);

            if (svc.Add(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = 1, msg = "失败" };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id, ProcedurePostDto dto)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            var entity = mapper.Map(dto, data);

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            data.Status = 0;

            if (svc.Update(data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        [HttpGet("BGet")]
        public PagedResult<BusinessInfo> BusGet() 
        {
            var sql = Bsvc.Query(o => true);
            return new PagedResult<BusinessInfo>() {code=1,msg="成功",data=sql };
        }
    }
}
